package org.hzero.order.api.controller.v1;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.hzero.order.config.SwaggerApiConfig;
import org.hzero.order.domain.entity.Company;
import org.hzero.order.domain.repository.CompanyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


/**
 * @ Description   :  公司管理 API
 * @ Author        :  xiong.yilin@hand-china.com
 * @ CreateDate    :  2019/8/8 10:27
 */
@Api(tags = SwaggerApiConfig.COMPANY)
@RestController("companyController.v1")
@RequestMapping("/v1/{organizationId}/companys")//RequestMapping是一个用来处理请求地址映射的注解
public class CompanyController extends BaseController {

    @Autowired
    private CompanyRepository companyRepository;

    @ApiOperation(value = "公司列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    //@ApiIgnore 加注解在类或者方法上 表示在swagger页面,忽略掉这个类或者方法,不显示
    public ResponseEntity<Page<Company>> list
            (@PathVariable("organizationId") Long tenantId, Company company,
             @ApiIgnore @SortDefault(value = Company.FIELD_COMPANY_ID,
                     direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<Company> list = companyRepository.pageAndSort(pageRequest, company);
        return Results.success(list);
    }

    @ApiOperation(value = "公司明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{companyId}")
    public ResponseEntity<Company> detail(@PathVariable Long companyId) {
        Company company = companyRepository.selectByPrimaryKey(companyId);
        return Results.success(company);
    }

    @ApiOperation(value = "创建公司")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<Company> create(@RequestBody Company company) {
        // 简单数据校验
        validObject(company);
        companyRepository.insertSelective(company);
        return Results.success(company);
    }

    @ApiOperation(value = "修改公司")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PutMapping
    public ResponseEntity<Company> update(@RequestBody Company company) {
        // 数据防篡改校验
        SecurityTokenHelper.validToken(company);
        companyRepository.updateByPrimaryKeySelective(company);
        return Results.success(company);
    }

    @ApiOperation(value = "删除公司")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody Company company) {
        SecurityTokenHelper.validToken(company);
        companyRepository.deleteByPrimaryKey(company);
        return Results.success();
    }

}
